package com.wpp.config;

import com.wpp.constant.Constant;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @Classname ZkDataSourceCenter
 * @Description TODO
 * @Date 2020/12/9 23:19
 * @Created by wpp25
 */
public class ZkConfigCenter implements ConfigCenter{

    private final CuratorFramework curatorFramework;

    public CuratorFramework getCuratorFramework() {
        return curatorFramework;
    }

    public ZkConfigCenter() {
        this.curatorFramework = CuratorFrameworkFactory.builder()
                .retryPolicy(new ExponentialBackoffRetry(1000,3))
                .connectString(Constant.ZkConfig.IP_LOCALHOST)
                .sessionTimeoutMs(50000)
                .connectionTimeoutMs(30000)
                .build();
        this.curatorFramework.start();
    }

    @Override
    public Properties loadConfig() {
        Properties properties = new Properties();
        String configRoot = Constant.ZkConfig.CONFIG_ROOT;

        try {
            initDataSourceConfig();
            List<String> datasourceConfig = curatorFramework.getChildren().forPath(configRoot);
            for (String configKey : datasourceConfig) {
                String configPath = configRoot + Constant.ZkConfig.SPILT +configKey;
                byte[] configValue = curatorFramework.getData().forPath(configPath);
                properties.setProperty(configKey,new String(configValue));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return properties;
    }

    // TODO: 2020/12/9
    @Override
    public Properties refreshConfig() {
        Properties properties = new Properties();



        return properties;
    }

    @Override
    public void initDataSourceConfig() {
        Properties properties = new Properties();
        properties.setProperty("spring.datasource.driver-class-name","com.mysql.jdbc.Driver");
        // ?characterEncoding=utf-8&serverTimezone=GMT
        properties.setProperty("spring.datasource.url","jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=GMT");
        properties.setProperty("spring.datasource.username","root");
        properties.setProperty("spring.datasource.password","root");

        Set<Map.Entry<Object, Object>> entries = properties.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {
            // 注册的配置路径
            String configPath = Constant.ZkConfig.CONFIG_ROOT + Constant.ZkConfig.SPILT + entry.getKey();
            // 判断节点是否存在 不存在则 创建配置的永久节点
            try {
                if (curatorFramework.checkExists().forPath(configPath) == null) {
                    curatorFramework.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT)
                            .forPath(configPath,entry.getValue().toString().getBytes());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
